1 00:00:01,280 --> 00:00:06,530 In this video, I'm going to demonstrate the touched event and connect it to a function to make a part 2 00:00:06,530 --> 00:00:07,320 disappear. 3 00:00:07,340 --> 00:00:09,560 It's the basic template of a pickup. 4 00:00:09,560 --> 00:00:10,870 So there's my part. 5 00:00:10,880 --> 00:00:11,990 I'm going to touch it. 6 00:00:11,990 --> 00:00:12,680 Boom. 7 00:00:12,680 --> 00:00:14,420 It's gone pretty cool. 8 00:00:14,420 --> 00:00:18,350 And then later we can add like cool stuff, like superpowers and things like that. 9 00:00:18,350 --> 00:00:19,910 But first we've got to get the basics. 10 00:00:19,910 --> 00:00:23,570 Let's turn this off and then get a fresh world and get started. 11 00:00:24,740 --> 00:00:26,620 I have an empty world here. 12 00:00:26,630 --> 00:00:27,770 Nothing in it. 13 00:00:27,800 --> 00:00:29,930 Let's go to part. 14 00:00:30,290 --> 00:00:32,810 Hit the little arrow and then add a sphere. 15 00:00:32,840 --> 00:00:35,510 That's a good thing for a pickup right here. 16 00:00:35,540 --> 00:00:38,120 Let's go to that part and name it. 17 00:00:38,840 --> 00:00:39,530 Pickup. 18 00:00:40,100 --> 00:00:40,920 Cool beans. 19 00:00:40,940 --> 00:00:46,340 Let's make a brick color red and then we'll go to material. 20 00:00:46,340 --> 00:00:47,350 We want it to glow. 21 00:00:47,360 --> 00:00:48,110 So I'm gonna say. 22 00:00:48,260 --> 00:00:52,040 And for neon, this is just a static, so it's not going to change anything. 23 00:00:52,650 --> 00:00:54,740 And let's go to size. 24 00:00:54,740 --> 00:00:58,400 Maybe we'll make a one by one by one instead of four by four by four. 25 00:00:58,550 --> 00:01:02,510 A little more reasonable for a pickup to that move. 26 00:01:02,750 --> 00:01:04,010 Move it up a little bit. 27 00:01:05,050 --> 00:01:06,400 That's pretty good. 28 00:01:08,070 --> 00:01:08,790 Anchored. 29 00:01:08,790 --> 00:01:11,510 Let's anchor it in place so it doesn't fall down. 30 00:01:11,520 --> 00:01:16,890 But I also want to be able to run through it so above anchored can collide. 31 00:01:16,890 --> 00:01:21,110 I'm going to uncheck the touched event will still fire, right? 32 00:01:21,120 --> 00:01:25,080 So you don't have to worry about that but if you forget to anchor it and can collide is off. 33 00:01:25,080 --> 00:01:27,390 It's going to fall through your ground, it's going to disappear. 34 00:01:27,390 --> 00:01:28,980 So just be careful with that. 35 00:01:29,280 --> 00:01:29,610 All right. 36 00:01:29,610 --> 00:01:33,450 So on our pickup, let's hit the plus sign script. 37 00:01:34,990 --> 00:01:37,930 And let's call this simple pickup. 38 00:01:40,210 --> 00:01:40,900 Cool. 39 00:01:41,050 --> 00:01:42,550 Let's go here. 40 00:01:42,790 --> 00:01:48,340 Get rid of that print statement in simple pickup and we'll get a variable for our pickup. 41 00:01:48,730 --> 00:01:52,120 I'll call a pickup with a lowercase P script. 42 00:01:52,450 --> 00:01:53,030 Parent. 43 00:01:53,050 --> 00:01:53,590 There we go. 44 00:01:53,590 --> 00:02:01,210 That puts a reference of our part into our variable, say, local function on touch. 45 00:02:01,240 --> 00:02:04,240 Now you can name this whatever you want, but it should make sense. 46 00:02:04,510 --> 00:02:07,150 But it's going to be catching the touched event. 47 00:02:07,150 --> 00:02:07,750 So on. 48 00:02:07,750 --> 00:02:08,740 Touched as good. 49 00:02:09,130 --> 00:02:16,150 Other part is going to be the variable that's passed into on touch when the pickup is touched. 50 00:02:16,150 --> 00:02:17,840 So you have two parts going on here, right? 51 00:02:17,860 --> 00:02:20,650 You got the pickup and then you've got the other part to touch the pickup. 52 00:02:21,100 --> 00:02:22,750 That's a little bit tricky sometimes. 53 00:02:23,260 --> 00:02:23,550 All right. 54 00:02:23,560 --> 00:02:31,060 Now down here below the on touch function, let's get the pickup dot touch event. 55 00:02:31,090 --> 00:02:32,350 See that little lightning bolt? 56 00:02:32,440 --> 00:02:33,730 That means it's an event. 57 00:02:34,940 --> 00:02:39,560 We're going to do a colon connect to on touch. 58 00:02:39,770 --> 00:02:41,030 Why use the dot? 59 00:02:41,030 --> 00:02:42,560 Why use the colon? 60 00:02:42,560 --> 00:02:44,180 What colons are for methods? 61 00:02:44,180 --> 00:02:45,710 Dots are for functions. 62 00:02:45,710 --> 00:02:47,600 They're pretty similar in Lua. 63 00:02:47,600 --> 00:02:51,410 You're just going to have to kind of get used to it and you'll start figuring it out. 64 00:02:51,410 --> 00:02:55,100 But it's going to take practice like a good definition is not going to help you. 65 00:02:55,130 --> 00:02:56,420 You're just going to have to know. 66 00:02:56,840 --> 00:02:57,200 All right. 67 00:02:57,200 --> 00:03:01,340 So in my what's going to happen is a pickup is going to fire the touch event when it's touched, it's 68 00:03:01,340 --> 00:03:03,680 going to connect to on touch function. 69 00:03:03,680 --> 00:03:08,000 Roberts is going to know, hey, this is this is a touched event. 70 00:03:08,000 --> 00:03:12,380 So we need the other part that's going to come, the parameter, the variable that gets passed in. 71 00:03:12,680 --> 00:03:17,360 And then we can do a print to see what touched our part. 72 00:03:18,480 --> 00:03:19,980 Oh, just did print twice. 73 00:03:20,450 --> 00:03:23,240 Other part is. 74 00:03:24,700 --> 00:03:26,130 Other part name. 75 00:03:26,800 --> 00:03:31,060 And now that's going to print out what touched our pickup. 76 00:03:31,240 --> 00:03:34,420 Let's go to view output play. 77 00:03:37,220 --> 00:03:38,240 There's our part. 78 00:03:38,480 --> 00:03:39,200 Run up. 79 00:03:39,890 --> 00:03:40,160 Look at that. 80 00:03:40,160 --> 00:03:41,950 We got a lot of things that just got printed out. 81 00:03:41,960 --> 00:03:44,840 Right hand, right lower arm handle. 82 00:03:44,870 --> 00:03:46,280 I'll explain what that is later. 83 00:03:46,700 --> 00:03:48,120 Right upper arm. 84 00:03:48,140 --> 00:03:50,330 Let's go to our workspace. 85 00:03:50,330 --> 00:03:50,650 Right. 86 00:03:50,660 --> 00:03:51,690 See workspace. 87 00:03:51,710 --> 00:03:53,840 There's our character in there. 88 00:03:53,870 --> 00:03:55,140 Let's open that up. 89 00:03:55,160 --> 00:03:55,750 Look at that. 90 00:03:55,760 --> 00:03:56,930 That's a lot of those parts. 91 00:03:56,930 --> 00:03:58,220 Right, right hand. 92 00:03:58,940 --> 00:04:00,760 Right hand. 93 00:04:00,770 --> 00:04:01,760 Pretty cool. 94 00:04:01,790 --> 00:04:04,280 We're going to get to know this pretty well, right? 95 00:04:04,340 --> 00:04:07,100 I'm going to spend a lot of time on the character itself. 96 00:04:07,100 --> 00:04:12,260 So thing is, part didn't disappear and we touched it many, many, many times. 97 00:04:12,260 --> 00:04:17,390 We only want to touch it once, especially if it's giving superpowers or health and things like that. 98 00:04:17,750 --> 00:04:26,180 So let's go to our on touch when somebody touches it or something touches it because we don't have a 99 00:04:26,180 --> 00:04:29,420 filter right now, we are going to make it disappear. 100 00:04:29,910 --> 00:04:34,040 Say, pick up colon, there's our crazy colon again. 101 00:04:34,040 --> 00:04:35,570 Destroy. 102 00:04:35,660 --> 00:04:38,720 We're going to destroy our pickup when someone touches it. 103 00:04:39,560 --> 00:04:40,280 Let's try it. 104 00:04:41,760 --> 00:04:43,050 Or something right now. 105 00:04:43,050 --> 00:04:43,800 It could be anything. 106 00:04:43,800 --> 00:04:44,520 Anything could touch. 107 00:04:44,520 --> 00:04:45,660 You could kick a ball onto that. 108 00:04:45,660 --> 00:04:49,710 It would still be destroyed by that left hand touched it. 109 00:04:49,710 --> 00:04:50,880 It's gone. 110 00:04:50,880 --> 00:04:53,040 And the event only fired once. 111 00:04:53,430 --> 00:04:54,810 That's pretty good. 112 00:04:55,080 --> 00:04:55,530 All right. 113 00:04:55,530 --> 00:04:57,690 One more thing I want to show you in this video. 114 00:04:58,260 --> 00:05:00,240 Let's take a look at our script. 115 00:05:00,910 --> 00:05:01,830 Get rid of that. 116 00:05:02,700 --> 00:05:04,620 You can do this two ways. 117 00:05:04,980 --> 00:05:06,980 I'm going to duplicate this part. 118 00:05:06,990 --> 00:05:08,400 Let's go to base plate. 119 00:05:08,730 --> 00:05:09,850 Get the part. 120 00:05:09,870 --> 00:05:12,510 Collisions are off, such as duplicate in place. 121 00:05:12,510 --> 00:05:16,420 Control D drag this over here. 122 00:05:16,440 --> 00:05:19,770 Let's make this pick up two so we don't get it confused. 123 00:05:20,520 --> 00:05:21,540 Open that up. 124 00:05:21,900 --> 00:05:27,090 Simple pickup two for the script, and then maybe I'll just make this a different color. 125 00:05:27,240 --> 00:05:28,320 Like pink. 126 00:05:29,160 --> 00:05:29,760 All right. 127 00:05:29,760 --> 00:05:38,100 In our simple pickup to right, simple pickup two, I'm going to show you how to do an anonymous function. 128 00:05:38,100 --> 00:05:39,930 So I'm breaking the function out. 129 00:05:39,930 --> 00:05:41,580 I normally do that. 130 00:05:41,580 --> 00:05:44,010 I connect with the function name. 131 00:05:44,010 --> 00:05:46,230 But there's another way to do this. 132 00:05:46,710 --> 00:05:52,170 All right, so this on touch, you could use an anonymous function with no name. 133 00:05:53,610 --> 00:05:56,820 Just pass it in as an argument to the connect. 134 00:05:57,750 --> 00:06:04,850 We'll get this the meat of the on touch function control see pasted here. 135 00:06:05,930 --> 00:06:08,840 And now you don't need that external function with the name. 136 00:06:08,840 --> 00:06:13,910 The downside is you can only use this here when you have a named function. 137 00:06:13,910 --> 00:06:17,190 There's capability of calling it from all different types of places. 138 00:06:17,210 --> 00:06:22,400 Also, if you're in a place that does unit tests, you might want to break out your function so you 139 00:06:22,400 --> 00:06:24,500 could test the functions individually. 140 00:06:24,920 --> 00:06:25,280 All right. 141 00:06:25,280 --> 00:06:26,470 Let's try it, though. 142 00:06:26,480 --> 00:06:28,160 Let's make this a little bit different, though. 143 00:06:28,160 --> 00:06:32,020 Let's say pink, just so that we see that it was different. 144 00:06:32,030 --> 00:06:32,870 That's pink. 145 00:06:32,870 --> 00:06:33,800 Let's do pink. 146 00:06:34,870 --> 00:06:37,000 Pink Koo play. 147 00:06:40,380 --> 00:06:41,310 Here we go. 148 00:06:41,820 --> 00:06:43,800 View output. 149 00:06:43,830 --> 00:06:45,330 Let's get our pink one. 150 00:06:46,150 --> 00:06:47,100 That worked fine. 151 00:06:47,100 --> 00:06:47,370 Right. 152 00:06:47,370 --> 00:06:48,360 Right hand. 153 00:06:48,360 --> 00:06:49,340 And then this one. 154 00:06:49,350 --> 00:06:49,890 Boom. 155 00:06:49,890 --> 00:06:50,550 Work fine. 156 00:06:50,550 --> 00:06:51,630 Upper torso. 157 00:06:51,660 --> 00:06:53,880 So there's two ways of doing it. 158 00:06:53,880 --> 00:06:55,980 Like I said, I will do the named functions. 159 00:06:55,980 --> 00:06:57,930 I'm going to be more formal than less formal. 160 00:06:57,930 --> 00:07:00,330 So I'm going to use this style more. 161 00:07:00,330 --> 00:07:02,790 So I will see you in the next video.